package com.lsh.oms.provider.service.status;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.alibaba.fastjson.JSON;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.status.CancelOrderDto;
import com.lsh.oms.api.service.status.IModifyStatusService;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.enums.OrderStatus;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.exception.ExceptionStatus;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.service.handler.CancelOrderHandler;
import com.lsh.oms.core.service.order.OrderQueryService;
import com.lsh.oms.core.service.tool.ToolService;
import com.lsh.oms.provider.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * @author panxudong
 * @date 17/3/23
 */
@Service(protocol = "rest")
@Path("order/status")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
@Slf4j
public class OrderCancelServiceImpl extends BaseService implements IModifyStatusService {

    private static final Logger logger = LoggerFactory.getLogger(OrderCancelServiceImpl.class);

    @Autowired
    private CancelOrderHandler cancelOrderHandler;

    @Autowired
    private OrderQueryService orderQueryService;

    /**
     * 用户取消
     *
     * @param cancelOrder
     * @return
     */
    @POST
    @Path("cancel/user")
    @Override
    public CommonResult<Object> cancelOrderByUser(CancelOrderDto cancelOrder) {
        logger.info("user 取消订单 {}", JSON.toJSONString(cancelOrder));
        String logSign = ToolService.getLogSign();

        CommonResult<Object> commonResult = new CommonResult<>();
        try {
            commonResult = cancelOrderHandler.execute(cancelOrder, CancelOrderHandler.CancelType.USER, logSign, this.getVenderId());
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode() + ",异常信息: " + e.getMessage(), e);
        }
        return commonResult;
    }

    /**
     * 系统取消
     *
     * @param cancelOrder
     * @return
     */
    @POST
    @Path("cancel/system")
    @Override
    public CommonResult<Object> cancelOrderByMisOrderCode(CancelOrderDto cancelOrder) {
        logger.info("system 取消订单 {}", JSON.toJSONString(cancelOrder));

        OrderHead orderHead = orderQueryService.findByMisCodeValid(cancelOrder.getMisOrderNo(), false, false);

        if (orderHead == null) {
            return new CommonResult<>(ReturnCode.REQUEST_FAIL, "订单不存在");
        }
        // 子单取消
        if (orderHead.getOrderStatus() == OrderStatus.ORDER_SPLIT_CHECK.getIndex() && orderHead.getValid() == 0) {
            return new CommonResult<>(ReturnCode.REQUEST_FAIL, "订单拆单成功");
        }
        // 订单取消
        cancelOrder.setOrderCode(orderHead.getOrderCode());
        if(orderHead.getParentOrderCode() == 0L){
            cancelOrder.setMisCodeCancelFalg(true);
        }

        String logSign = ToolService.getLogSign();
        CommonResult<Object> commonResult = new CommonResult<>();
        try {
            commonResult = cancelOrderHandler.execute(cancelOrder, CancelOrderHandler.CancelType.MIS_CODE, logSign, this.getVenderId());
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode() + ",异常信息: " + e.getMessage(), e);
        }

        return commonResult;
    }

    /**
     * mis取消
     *
     * @param cancelOrder
     * @return
     */
    @POST
    @Path("cancel/mis")
    @Override
    public CommonResult<Object> cancelOrderByMis(CancelOrderDto cancelOrder) {
        logger.info("mis 取消订单 {}", JSON.toJSONString(cancelOrder));
        String logSign = ToolService.getLogSign();
        CommonResult<Object> commonResult = new CommonResult<>();
        try {
            commonResult = cancelOrderHandler.execute(cancelOrder, CancelOrderHandler.CancelType.MIS, logSign, this.getVenderId());
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode() + ",异常信息: " + e.getMessage(), e);
        }

        return commonResult;
    }

    /**
     * 临时取消(用户取消)
     *
     * @param cancelOrder
     * @return
     */
    @POST
    @Path("cancel/temp")
    @Override
    public CommonResult<Object> cancelOrderByMisTemp(CancelOrderDto cancelOrder) {
        String logSign = ToolService.getLogSign();
        CommonResult<Object> commonResult = new CommonResult<>();
        logger.info("temp 取消订单 {}", JSON.toJSONString(cancelOrder));
        try {
            commonResult = cancelOrderHandler.execute(cancelOrder, CancelOrderHandler.CancelType.TEMP, logSign, this.getVenderId());
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode() + ",异常信息: " + e.getMessage(), e);
        }

        return commonResult;
    }




    /**
     * 临时取消(用户取消)
     *
     * @param cancelOrder
     * @return
     */
    @POST
    @Path("dmall/cancel")
    @Override
    public CommonResult<Object> cancelOrderByDmall(CancelOrderDto cancelOrder) {
        String logSign = ToolService.getLogSign();
        CommonResult<Object> commonResult = new CommonResult<>();
        logger.info("dmall 取消订单 {}", JSON.toJSONString(cancelOrder));
        try {

           if (!this.getDmallKey().equals("dmall2020dmallmaxmz0218")){
                return new CommonResult<>(ReturnCode.REQUEST_FAIL, "验证失败");
            }

            if (StringUtils.isEmpty(cancelOrder.getDmallOrderCode())){
                return new CommonResult<>(ReturnCode.REQUEST_FAIL, "多点订单号必传");
            }

            OrderHead orderHead = orderQueryService.findByRemarks(cancelOrder.getDmallOrderCode(), false);

            if (orderHead == null) {
                return new CommonResult<>(ReturnCode.REQUEST_FAIL, cancelOrder.getDmallOrderCode() + "-订单信息不存在");
            }
            cancelOrder.setOrderCode(orderHead.getOrderCode());

            commonResult = cancelOrderHandler.execute(cancelOrder, CancelOrderHandler.CancelType.MIS, logSign, orderHead.getVenderId());
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode() + ",异常信息: " + e.getMessage(), e);
        }

        return commonResult;
    }
}
